<?php

namespace app\admin\controller;

use app\admin\model\Image as adminImage;
use app\admin\model\System as sysAdmin;
use think\Db;
use think\Request;
use app\admin\controller\Base;
use app\admin\model\Member as modelMember;


class Member extends base
{

    //
    public function index()
    {
        return $this->fetch();
    }
    //会员列表
    public function list(){
        $member=Db::name('member')->field(['id','username','realname','gender','user_money','daili','status','created_at'])->paginate(8);
        $this->assign('member',$member);

        return $this->fetch('list');
    }
    //会员添加
    public function add(Request $request){

        if($request->ispost()){
            $username = $request->param('user_name', 'trim');
            $password = $request->param('password','trim');
            $gender   = $request->param('gender');
            $daili    = $request->param('daili','trim');
            $realname = $request->param('realname','trim');
            $salt=mt_rand(10000,99999);
            $data = ['username' => $username,'daili'=>$daili, 'password_hash' => $password, 'salt' => $salt, 'realname' => $realname,'gender'=>$gender, 'created_at' => time()];

             $members=new modelMember;
             $res=$members->insertUser($data);
            if($res){
                $this->success('注册成功', '/admin/member/list');;
            }else{
                $this->success('注册失败', '/admin/member/add');;
            }
        }else{
            return $this->fetch('add');
        }
    }
    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        echo '<pre>';
        print_r($id);exit;
        $member=new modelMember();

        $member=$member->getmember($id);

        $this->assign('member',$member);

        return $this->fetch('update');
    }

    //会员信息修改
    public function update(Request $request, $id)
    {

        $username = $request->param('username', 'trim');
        $password = $request->param('password_hash','trim');
        $daili    = $request->param('daili','trim');
        $realname = $request->param('realname','trim');
        $salt=mt_rand(10000,99999);
        $data = ['username' => $username,'daili'=>$daili, 'password_hash' => $password, 'salt' => $salt, 'realname' => $realname,  'updated_at' => time()];

        $member=new modelMember;
        $res= $member->updateMember($id,$data);
        if($res){
            return $this->success('更新成功','admin/Member/list');
        }else{
            return $this->error('更新失败','admin/Member/list');
        }
    }
    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $member=new modelMember;
        $res=$member->deleteMember($id);
        if($res){
            return json(['status'=>'success','msg'=>'删除成功']);
        }else{
            return json(['status'=>'fail','msg'=>'删除失败' ]);
        }

    }



}
